package com.apache.rpc.client;

import com.apache.method.SystemRunnable;
import com.apache.method.ThreadPool;
import com.apache.rpc.common.RpcUtil;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;

import java.util.Map;

/**
 * description: 监控信保存线程
 *
 * @author dyh 创建时间：2018年03月09日
 */
public class SaveRunableRpcLogHelper {

    private ThreadPool taskPool;

    private static SaveRunableRpcLogHelper instance;

    private AbstractProtocolService socketService;

    private AbstractProtocolService httpService;

    private SaveRunableRpcLogHelper() {
        socketService = new SocketProtocolService();
        httpService = new HttpProtocolService();
        taskPool = new ThreadPool(2, "监控线程");
    }

    public synchronized static SaveRunableRpcLogHelper getInstance() {
        if (null == instance) {
            instance = new SaveRunableRpcLogHelper();
        }
        return instance;
    }

    public void exeMonitoring(Map map) {
        String beanId = String.valueOf(map.get("beanId"));
        if ("auditService".equalsIgnoreCase(beanId) || "rpcService"
                .equalsIgnoreCase(beanId)) {//不记录日志的接口
            return;
        }
        MonitoringDataBaseRunable runable = new MonitoringDataBaseRunable(map);
        taskPool.execute(runable);
    }

    class MonitoringDataBaseRunable extends SystemRunnable {

        private Map map;

        public MonitoringDataBaseRunable(Map map) {
            this.map = map;
        }

        public void run() {
            String serviceIp = ConfigUtil.getInstance().getValueByKey("registry_protocol");
            if (StrUtil.isNull(serviceIp)) {
                return;
            }
            String[] str = serviceIp.split(",");
            String address = str[0];
            long dd = RpcUtil.getInstance().getCountMap(address);
            for (int i = 1; i < str.length; i++) {
                if (RpcUtil.getInstance().getCountMap(str[i]) < dd) {
                    dd = RpcUtil.getInstance().getCountMap(str[i]);
                    address = str[i];
                }
            }
            RpcUtil.getInstance().setCountMap(address);
            InterfaceRegister register = new InterfaceRegister();
            if (address.startsWith("http:")) {
                register.setCallType("http");
                register.setAddress(address);
                httpService.setRegister(register);
                httpService.doService("rpcService", "saveRpcLog", map);
            } else {
                String[] regparam = address.split(":");
                if (RpcUtil.getInstance().isHostConnectable(regparam[0], regparam[1])) {
                    register.setAddress(regparam[0]);
                    register.setPort(regparam[1]);
                    register.setCallType("socket");
                    socketService.setRegister(register);
                    socketService.doService("rpcService", "saveRpcLog", map);
                }
            }
            RpcUtil.getInstance().delCountMap(address);
        }
    }
}
